package com.taxicalculator.dao;

import java.io.IOException;

import org.json.JSONArray;
import org.json.JSONObject;

import android.content.Context;
import android.location.Location;
/**
 * 
 * @author Thomas Maco
 *
 */
public class TaxiCalculatorDAO implements ITaxiCalculatorDAO {

	private String addressString;
	INetworkDAO networkDAO = new NetworkDAO();
	static double fromLatitude;
	static double fromLongitude;
	static double toLatitude;
	static double toLongitude;
	ILocationSQLite locationSQLite;
	private int toLocationType = 1;
	/**
	 * Will find the distance in miles between two locations
	 * @param x location From
	 * @param y location To
	 * @return float miles in between location x and location y
	 * @throws Exception
	 */
	@Override
	public double getDistance(String toAddress, Context cxt) throws Exception {
		
		double dist = 0;
		String uri = "http://maps.googleapis.com/maps/api/geocode/json?address=" + toAddress + "&sensor=true";
		String fixedURI = uri.replace(" ", "%20");
		try {
			// result will hold the JSON data.
			String result = networkDAO.request(fixedURI);
			

			//Put the JSON result in an Object
			JSONObject root = new JSONObject(result);
			
			//Takes the Results and puts them into an JSON Array
			JSONArray resultsArray = root.getJSONArray("results");
			
			//Takes the first object in the array makes it into a string
			String subRootResult = resultsArray.getString(0);
			
			//takes a string from above and makes it an JSON Object
			JSONObject types = new JSONObject(subRootResult);
			
			//Gets the address string from the object above by search from the formatted address string
			String supersubRootResults = types.getString("geometry");
			
			JSONObject locationResults = new JSONObject(supersubRootResults);
			
			String finalResults = locationResults.getString("location");
			//gets the last object out of the JSON
			JSONObject locationLatLng = new JSONObject(finalResults) ;
			//gets and assigns GEO points
			toLatitude= locationLatLng.getDouble("lat");
			toLongitude = locationLatLng.getDouble("lng");
			//create two locations with latitude and longitude points 
			Location locationA = new Location("A");
			locationA.setLatitude(fromLatitude);
			locationA.setLongitude(fromLongitude);
			Location locationB = new Location("B");
			locationB.setLatitude(toLatitude);
			locationB.setLongitude(toLongitude);

			//this gets the distance bewteen to locations in meters
			dist = locationA.distanceTo(locationB);
			//converts meters into miles
			dist = dist / 1609.34; 
			
			locationSQLite = new LocationsSQLite(cxt);
			locationSQLite.saveLocation(toAddress, toLocationType, toLatitude, toLongitude);
	
		} catch (Exception e) {
			e.printStackTrace();
		}

		
		//returns distance to be calculated
		return dist;
	}

	/**
	 * fetches old addresses for the From from the database
	 * @return list of all addresses in database
	 * @throws Exception
	 */
	@Override
	public String getAddress(double latitude, double longitude)throws Exception {
		fromLatitude = latitude;
		fromLongitude = longitude;
		
		String subRootResult;
		try {
			// the query URI we wish to access.
			String uri = "http://maps.googleapis.com/maps/api/geocode/json?latlng="	+ latitude + "," + longitude + "&sensor=true";

			// result will hold the JSON data.
			String result = networkDAO.request(uri);
			

			//Put the JSON result in an Object
			JSONObject root = new JSONObject(result);
			
			//Takes the Results and puts them into an JSON Array
			JSONArray resultsArray = root.getJSONArray("results");
			
			//Takes the first object in the array makes it into a string
			subRootResult = resultsArray.getString(0);
			
			//takes a string from above and makes it an JSON Object
			JSONObject types = new JSONObject(subRootResult);
			
			//Gets the address string from the object above by search from the formatted address string
			addressString =types.getString("formatted_address");
			 
			
		}
		catch (IOException e) {

			addressString = "";
		}
		return addressString;
	}
	

}
